package com.base.cn.platform.os.controller.user.account;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.entity.manage.config.SysConfig;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.user.account.CusUserAccountHistoryService;
import com.base.cn.platform.os.service.manage.user.account.CusUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户账户接口
 *
 * @author s.li
 * @create 2018-09-05-15:07
 */
@RestController
@RequestMapping("/api/account")
public class ApiUserAccountController extends ApiBaseController {

    @Autowired
    private CusUserAccountService cusUserAccountService;
    @Autowired
    private CusUserAccountHistoryService cusUserAccountHistoryService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 个⼈账户
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/myAccount")
    public Map<String,Object> myAccount(@RequestParam("userId") BigDecimal userId){
        Map<String,Object> userAccount = cusUserAccountService.userAccountInfo(userId,false);
        boolean gotMoneySwitch =false;
        TreeMap<String, String> sysConfigContext = sysConfigService.getSysConfigContext(ConfigName.ONOFF);
        if(StringUtils.isNotEmpty(sysConfigContext.get("iosWithdraw"))&&sysConfigContext.get("iosWithdraw").equals("ON")){
            gotMoneySwitch=true;
        }
        userAccount.put("gotMoneySwitch",gotMoneySwitch);
        return ResultUtil.SUCCESS(userAccount);
    }

    /**
     * 个⼈消费记录
     * @param userId 用户ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/queryUserAccountHistories")
    public Map<String,Object> queryUserAccountHistories(@RequestParam("userId") BigDecimal userId,
                                                        @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        if(!DataUtil.idIsNotNull(userId)){
           return ResultUtil.ERROR("请登录再操作");
        }
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        params.put("isDisplay",0);
        Map<String,Object> accountHistoryMap = cusUserAccountHistoryService.userAccountHistoryListPage(params,currentPage,false);
        return ResultUtil.SUCCESS(accountHistoryMap);
    }
}
